package com.zj.easy.utils;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.tencent.mmkv.MMKV;
import com.wanjian.cockroach.Cockroach;
import com.wanjian.cockroach.ExceptionHandler;
import com.zj.easy.utils.app.ActivityManager;
import com.zj.easy.utils.logger.XLogger;

public class UtilsInit {
    public static Application app;

    public static void init(Application context) {
        app = context;
        //初始化cockroach
        if (!BuildConfig.DEBUG) {
            initCockroach(context);
        }
        //初始化activitymanager
        context.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
                ActivityManager.getInstance().onActivityCreated(activity, savedInstanceState);
            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {
                ActivityManager.getInstance().onActivityStarted(activity);
            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {
                ActivityManager.getInstance().onActivityResumed(activity);
            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {
                ActivityManager.getInstance().onActivityPaused(activity);
            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {
                ActivityManager.getInstance().onActivityStopped(activity);
            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
                ActivityManager.getInstance().onActivitySaveInstanceState(activity, outState);
            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {
                ActivityManager.getInstance().onActivityDestroyed(activity);
            }
        });
        //设定log
        if (BuildConfig.DEBUG) {
            XLogger.debug("=-=");
        }
        //初始化mmkv
        MMKV.initialize(context);

    }

    /**
     * 初始化cockroach
     */
    private static void initCockroach(Context context) {
        Cockroach.install(context, new ExceptionHandler() {
            @Override
            protected void onUncaughtExceptionHappened(Thread thread, Throwable throwable) {

            }

            @Override
            protected void onBandageExceptionHappened(Throwable throwable) {

            }

            @Override
            protected void onEnterSafeMode() {

            }

            @Override
            protected void onMayBeBlackScreen(Throwable e) {
                super.onMayBeBlackScreen(e);
                //黑屏时建议直接杀死app
                System.exit(0);
            }

        });
    }


}
